package cn.iocoder.an.module.mold.controller.admin.upkeepitem;

import cn.iocoder.an.module.mold.controller.admin.upkeepcat2item.vo.UpkeepCat2itemRespVO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcat2item.UpkeepCat2itemDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.upkeepitem.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepitem.UpkeepItemDO;
import cn.iocoder.an.module.mold.service.upkeepitem.UpkeepItemService;

@Tag(name = "管理后台 - 模具保养项目")
@RestController
@RequestMapping("/mold/upkeep-item")
@Validated
public class UpkeepItemController {

    @Resource
    private UpkeepItemService upkeepItemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具保养项目")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:create')")
    public CommonResult<Long> createUpkeepItem(@Valid @RequestBody UpkeepItemSaveReqVO createReqVO) {
        return success(upkeepItemService.createUpkeepItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具保养项目")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:update')")
    public CommonResult<Boolean> updateUpkeepItem(@Valid @RequestBody UpkeepItemSaveReqVO updateReqVO) {
        upkeepItemService.updateUpkeepItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具保养项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:delete')")
    public CommonResult<Boolean> deleteUpkeepItem(@RequestParam("id") Long id) {
        upkeepItemService.deleteUpkeepItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具保养项目")
                @PreAuthorize("@ss.hasPermission('mold:upkeep-item:delete')")
    public CommonResult<Boolean> deleteUpkeepItemList(@RequestParam("ids") List<Long> ids) {
        upkeepItemService.deleteUpkeepItemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具保养项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:query')")
    public CommonResult<UpkeepItemRespVO> getUpkeepItem(@RequestParam("id") Long id) {
        UpkeepItemDO upkeepItem = upkeepItemService.getUpkeepItem(id);
        return success(BeanUtils.toBean(upkeepItem, UpkeepItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具保养项目分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:query')")
    public CommonResult<PageResult<UpkeepItemRespVO>> getUpkeepItemPage(@Valid UpkeepItemPageReqVO pageReqVO) {
        PageResult<UpkeepItemDO> pageResult = upkeepItemService.getUpkeepItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpkeepItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具保养项目 Excel")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpkeepItemExcel(@Valid UpkeepItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpkeepItemDO> list = upkeepItemService.getUpkeepItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具保养项目.xls", "数据", UpkeepItemRespVO.class,
                        BeanUtils.toBean(list, UpkeepItemRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得模具保养分类项目对照")
    public CommonResult<List<UpkeepItemDO>> getUpkeepCat2itemByCatId() {
        List<UpkeepItemDO> list = upkeepItemService.getUpkeepItemAll();
        return success(BeanUtils.toBean(list, UpkeepItemDO.class));
    }
}